home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / etc / init.d / checkroot.sh < prev    next >
Encoding:
Linux/UNIX/POSIX Shell Script  |  2012-03-27  |  10.6 KB  |  437 lines

  1. #! /bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:          checkroot
  4. # Required-Start:    mountdevsubfs hostname
  5. # Required-Stop:     
  6. # Should-Start:      keymap hwclockfirst hdparm bootlogd
  7. # Should-stop:
  8. # Default-Start:     S
  9. # Default-Stop:
  10. # X-Interactive:     true
  11. # Short-Description: Check to root file system.
  12. ### END INIT INFO
  13.  
  14. # Include /usr/bin in path to find on_ac_power if /usr/ is on the root
  15. # partition.
  16. PATH=/sbin:/bin:/usr/bin
  17. FSCK_LOGFILE=/var/log/fsck/checkroot
  18. [ "$FSCKFIX" ] || FSCKFIX=no
  19. [ "$SULOGIN" ] || SULOGIN=no
  20. . /lib/init/vars.sh
  21.  
  22. . /lib/lsb/init-functions
  23. . /lib/init/mount-functions.sh
  24. . /lib/init/splash-functions-base
  25. . /lib/init/usplash-fsck-functions.sh
  26.  
  27. do_start () {
  28.     #
  29.     # Set SULOGIN in /etc/default/rcS to yes if you want a sulogin to
  30.     # be spawned from this script *before anything else* with a timeout,
  31.     # like sysv does.
  32.     #
  33.     [ "$SULOGIN" = yes ] && sulogin -t 30 $CONSOLE
  34.  
  35.     KERNEL="$(uname -s)"
  36.     MACHINE="$(uname -m)"
  37.  
  38.     #
  39.     # Read /etc/fstab, looking for:
  40.     # 1) The root filesystem, resolving LABEL=*|UUID=* entries to the
  41.     #     device node,
  42.     # 2) Swap that is on a md device or a file that may be on a md 
  43.     #     device,
  44.     #
  45.  
  46.     exec 9<&0 </etc/fstab
  47.  
  48.     fstabroot=/dev/root
  49.     rootdev=none
  50.     roottype=none
  51.     rootopts=defaults
  52.     rootmode=rw
  53.     rootcheck=no
  54.     swap_on_lv=no
  55.     swap_on_file=no
  56.  
  57.     while read DEV MTPT FSTYPE OPTS DUMP PASS JUNK
  58.     do
  59.         case "$DEV" in
  60.           ""|\#*)
  61.             continue;
  62.             ;;
  63.           /dev/mapper/*)
  64.             [ "$FSTYPE" = "swap" ] && swap_on_lv=yes
  65.             ;;
  66.           /dev/*)
  67.             ;;
  68.           LABEL=*|UUID=*)
  69.             if [ "$MTPT" = "/" ] && [ -x /sbin/findfs ]
  70.             then
  71.                 DEV="$(findfs "$DEV")"
  72.             fi
  73.             ;;
  74.           /*)
  75.             [ "$FSTYPE" = "swap" ] && swap_on_file=yes
  76.             ;;
  77.           *)
  78.             ;;
  79.         esac
  80.         [ "$MTPT" != "/" ] && continue
  81.         rootdev="$DEV"
  82.         fstabroot="$DEV"
  83.         rootopts="$OPTS"
  84.         roottype="$FSTYPE"
  85.         ( [ "$PASS" != 0 ] && [ "$PASS" != "" ]   ) && rootcheck=yes
  86.         ( [ "$FSTYPE" = "nfs" ] || [ "$FSTYPE" = "nfs4" ] ) && rootcheck=no
  87.         case "$OPTS" in
  88.           ro|ro,*|*,ro|*,ro,*)
  89.             rootmode=ro
  90.             ;;
  91.         esac
  92.     done
  93.  
  94.     exec 0<&9 9<&-
  95.  
  96.     #
  97.     # Activate the swap device(s) in /etc/fstab. This needs to be done
  98.     # before fsck, since fsck can be quite memory-hungry.
  99.     #
  100.     ENABLE_SWAP=no
  101.     case "$KERNEL" in
  102.       Linux)
  103.           if [ "$NOSWAP" = yes ]
  104.         then
  105.             [ "$VERBOSE" = no ] || log_warning_msg "Not activating swap as requested via bootoption noswap."
  106.             ENABLE_SWAP=no
  107.         else
  108.             if [ "$swap_on_lv" = yes ]
  109.             then
  110.                 [ "$VERBOSE" = no ] || log_warning_msg "Not activating swap on logical volume."
  111.             elif [ "$swap_on_file" = yes ]
  112.             then
  113.                 [ "$VERBOSE" = no ] || log_warning_msg "Not activating swap on swapfile."
  114.             else
  115.                 ENABLE_SWAP=yes
  116.             fi
  117.         fi
  118.         ;;
  119.       *)
  120.         ENABLE_SWAP=yes
  121.         ;;
  122.     esac
  123.     if [ "$ENABLE_SWAP" = yes ]
  124.     then
  125.         if [ "$VERBOSE" = no ]
  126.         then
  127.             log_action_begin_msg "Activating swap"
  128.             swapon -a -e >/dev/null 2>&1
  129.             log_action_end_msg $?
  130.         else
  131.             log_daemon_msg "Activating swap"
  132.             swapon -a -v
  133.             log_end_msg $?
  134.         fi
  135.     fi
  136.  
  137.     #
  138.     # Does the root device in /etc/fstab match with the actual device ?
  139.     # If not we try to use the /dev/root alias device, and if that
  140.     # fails we create a temporary node in /lib/init/rw.
  141.     #
  142.     if [ "$rootcheck" = yes ]
  143.     then
  144.         ddev="$(mountpoint -qx $rootdev)"
  145.         rdev="$(mountpoint -d /)"
  146.         if [ "$ddev" != "$rdev" ] && [ "$ddev" != "4:0" ]
  147.         then
  148.             if [ "$(mountpoint -qx /dev/root)" = "4:0" ]
  149.             then
  150.                 rootdev=/dev/root
  151.             else
  152.                 if \
  153.                     rm -f /lib/init/rw/rootdev \
  154.                     && mknod -m 600 /lib/init/rw/rootdev b ${rdev%:*} ${rdev#*:} \
  155.                     && [ -e /lib/init/rw/rootdev ]
  156.                 then
  157.                     rootdev=/lib/init/rw/rootdev
  158.                 else
  159.                     rootfatal=yes
  160.                 fi
  161.             fi
  162.         fi
  163.     fi
  164.  
  165.     #
  166.     # Bother, said Pooh.
  167.     #
  168.     if [ "$rootfatal" = yes ]
  169.     then
  170.         log_failure_msg "The device node $rootdev for the root filesystem is missing or incorrect 
  171. or there is no entry for the root filesystem listed in /etc/fstab. 
  172. The system is also unable to create a temporary node in /lib/init/rw. 
  173. This means you have to fix the problem manually."
  174.         log_warning_msg "A maintenance shell will now be started. 
  175. CONTROL-D will terminate this shell and restart the system."
  176.         # Start a single user shell on the console
  177.         if ! sulogin $CONSOLE
  178.         then
  179.             log_failure_msg "Attempt to start maintenance shell failed. 
  180. Will restart in 5 seconds."
  181.             sleep 5
  182.         fi
  183.         [ "$VERBOSE" = no ] || log_action_msg "Will now restart"
  184.         reboot -f
  185.     fi
  186.  
  187.     # See if we're on AC Power.  If not, we're not gonna run our
  188.     # check.  If on_ac_power (in /usr/) is unavailable, behave as
  189.     # before and check all file systems needing it.
  190. # Disabled AC power check until fsck can be told to only check the
  191. # file system if it is corrupt when running on battery. (bug #526398)
  192. #    if which on_ac_power >/dev/null 2>&1 && [ "$rootcheck" = yes ]
  193. #    then
  194. #        on_ac_power >/dev/null 2>&1
  195. #        if [ "$?" -eq 1 ]
  196. #        then
  197. #            log_warning_msg "On battery power, so skipping file system check."
  198. #            rootcheck=no
  199. #        fi
  200. #    fi
  201.  
  202.     #
  203.     # See if we want to check the root file system.
  204.     #
  205.     FSCKCODE=0
  206.     if [ -f /fastboot ] || grep -s -w -i "fastboot" /proc/cmdline
  207.     then
  208.         [ "$rootcheck" = yes ] && log_warning_msg "Fast boot enabled, so skipping root file system check."
  209.         rootcheck=no
  210.     fi
  211.  
  212.     if [ "$rootcheck" = yes ]
  213.     then
  214.         #
  215.         # Ensure that root is quiescent and read-only before fsck'ing.
  216.         #
  217.         # mount -n -o remount,ro / would be the correct syntax but
  218.         # mount can get confused when there is a "bind" mount defined
  219.         # in fstab that bind-mounts "/" somewhere else.
  220.         #
  221.         # So we use mount -n -o remount,ro $rootdev / but that can
  222.         # fail on older kernels on sparc64/alpha architectures due
  223.         # to a bug in sys_mount().
  224.         #
  225.         # As a compromise we try both.
  226.         #
  227.         if \
  228.             ! mount    -n -o remount,ro              $rootdev /              \
  229.             && ! mount -n -o remount,ro -t dummytype $rootdev /  2>/dev/null \
  230.             && ! mount -n -o remount,ro                       /  2>/dev/null
  231.         then
  232.             log_failure_msg "Cannot check root file system because it is not mounted read-only."
  233.             rootcheck=no
  234.         fi
  235.     fi
  236.  
  237.     #
  238.     # The actual checking is done here.
  239.     #
  240.     if [ "$rootcheck" = yes ]
  241.     then
  242.         if [ -f /forcefsck ] || grep -s -w -i "forcefsck" /proc/cmdline
  243.         then
  244.             force="-f"
  245.         else
  246.             force=""
  247.         fi
  248.  
  249.         if [ "$FSCKFIX" = yes ]
  250.         then
  251.             fix="-y"
  252.         else
  253.             fix="-a"
  254.         fi
  255.  
  256.         spinner="-C"
  257.         case "$TERM" in
  258.           dumb|network|unknown|"")
  259.             spinner="" ;;
  260.         esac
  261.         # This Linux/s390 special case should go away.
  262.         if [ "${KERNEL}:${MACHINE}" = Linux:s390 ]
  263.         then
  264.             spinner=""
  265.         fi
  266.         
  267.         if [ "$VERBOSE" = no ]
  268.         then
  269.             log_action_begin_msg "Checking root file system"
  270.             if [ "$roottype" = "ext2" -o "$roottype" = "ext3" -o "$roottype" = "ext4" ] && usplash_running; then
  271.                 PROGRESS_FILE=`mktemp -p /lib/init/rw` || PROGRESS_FILE=/lib/init/rw/checkroot_fsck
  272.                 set -m
  273.                 logsave -s $FSCK_LOGFILE fsck -C3 $force $fix -t $roottype $rootdev >/dev/console 2>&1 3>$PROGRESS_FILE &
  274.                 set +m
  275.                 usplash_progress "$PROGRESS_FILE"
  276.                 rm -f $PROGRESS_FILE
  277.             else
  278.                 splash_start_indefinite
  279.                 logsave -s $FSCK_LOGFILE fsck $spinner $force $fix -t $roottype $rootdev
  280.                 FSCKCODE=$?
  281.                 splash_stop_indefinite
  282.             fi
  283.             if [ "$FSCKCODE" = 0 ]
  284.             then
  285.                 log_action_end_msg 0
  286.             else
  287.                 log_action_end_msg 1 "code $FSCKCODE"
  288.             fi
  289.         else
  290.             splash_start_indefinite
  291.             log_daemon_msg "Will now check root file system"
  292.             logsave -s $FSCK_LOGFILE fsck $spinner $force $fix -V -t $roottype $rootdev
  293.             FSCKCODE=$?
  294.             log_end_msg $FSCKCODE
  295.             splash_stop_indefinite
  296.         fi
  297.     fi
  298.  
  299.     #
  300.     # If there was a failure, drop into single-user mode.
  301.     #
  302.     # NOTE: "failure" is defined as exiting with a return code of
  303.     # 4 or larger. A return code of 1 indicates that file system
  304.     # errors were corrected but that the boot may proceed. A return
  305.     # code of 2 or 3 indicates that the system should immediately reboot.
  306.     #
  307.     if [ "$FSCKCODE" -gt 3 ]
  308.     then
  309.         # Surprise! Re-directing from a HERE document (as in "cat << EOF")
  310.         # does not work because the root is currently read-only.
  311.         log_failure_msg "An automatic file system check (fsck) of the root filesystem failed. 
  312. A manual fsck must be performed, then the system restarted. 
  313. The fsck should be performed in maintenance mode with the 
  314. root filesystem mounted in read-only mode."
  315.         log_warning_msg "The root filesystem is currently mounted in read-only mode. 
  316. A maintenance shell will now be started. 
  317. After performing system maintenance, press CONTROL-D 
  318. to terminate the maintenance shell and restart the system."
  319.         # Start a single user shell on the console
  320.         if ! sulogin $CONSOLE
  321.         then
  322.             log_failure_msg "Attempt to start maintenance shell failed. 
  323. Will restart in 5 seconds."
  324.             sleep 5
  325.         fi
  326.         [ "$VERBOSE" = no ] || log_action_msg "Will now restart"
  327.         reboot -f
  328.     elif [ "$FSCKCODE" -gt 1 ]
  329.     then
  330.         log_failure_msg "The file system check corrected errors on the root partition 
  331. but requested that the system be restarted."
  332.         log_warning_msg "The system will be restarted in 5 seconds."
  333.         sleep 5
  334.         [ "$VERBOSE" = no ] || log_action_msg "Will now restart"
  335.         reboot -f
  336.     fi
  337.  
  338.     #
  339.     # Remount root to final mode (rw or ro).
  340.     #
  341.     # See the comments above at the previous "mount -o remount"
  342.     # for an explanation why we try this twice.
  343.     #
  344.     if ! mount -n -o remount,$rootopts,$rootmode $fstabroot / 2>/dev/null
  345.     then
  346.         mount -n -o remount,$rootopts,$rootmode /
  347.     fi
  348.  
  349.     #
  350.     # We only create/modify /etc/mtab if the location where it is
  351.     # stored is writable. If /etc/mtab is a symlink into /proc/
  352.     # then it is not writable.
  353.     #
  354.     INIT_MTAB_FILE=no
  355.     MTAB_PATH="$(readlink -f /etc/mtab || :)"
  356.     case "$MTAB_PATH" in
  357.       /proc/*)
  358.         ;;
  359.       /*)
  360.         if touch "$MTAB_PATH" >/dev/null 2>&1
  361.         then
  362.             :> "$MTAB_PATH"
  363.             rm -f ${MTAB_PATH}~
  364.             INIT_MTAB_FILE=yes
  365.         fi
  366.         ;;
  367.       "")
  368.         [ -L /etc/mtab ] && MTAB_PATH="$(readlink /etc/mtab)"
  369.         if [ "$MTAB_PATH" ]
  370.         then
  371.             log_failure_msg "Cannot initialize ${MTAB_PATH}."
  372.         else
  373.             log_failure_msg "Cannot initialize /etc/mtab."
  374.         fi
  375.         ;;
  376.       *)
  377.         log_failure_msg "Illegal mtab location '${MTAB_PATH}'."
  378.         ;;
  379.     esac
  380.  
  381.     if [ "$INIT_MTAB_FILE" = yes ]
  382.     then
  383.         [ "$roottype" != none ] &&
  384.             mount -f -o $rootopts -t $roottype $fstabroot /
  385.     fi
  386.  
  387.     #
  388.     # Remove /lib/init/rw/rootdev if we created it.
  389.     #
  390.     rm -f /lib/init/rw/rootdev
  391. }
  392.  
  393. do_status () {
  394.     # If / is read-write or swap is enabled, this script have done
  395.     # its job.
  396.     rootrw=false
  397.     swapon=false
  398.     if [ -f /etc/mtab ] ; then
  399.         if grep " / " /etc/mtab |grep -q rw ; then
  400.         rootrw=true
  401.         fi
  402.     fi
  403.     if [ -f /proc/swaps ] ; then
  404.         if [ "$(cat /proc/swaps |grep -v ^Filename)" ] ; then
  405.         swapon=true
  406.         fi
  407.     fi
  408.     if [ true = "$rootrw" ] || [ true = "$swapon" ] ; then
  409.         return 0
  410.     else
  411.         return 4
  412.     fi
  413. }
  414.  
  415. case "$1" in
  416.   start|"")
  417.     do_start
  418.     ;;
  419.   restart|reload|force-reload)
  420.     echo "Error: argument '$1' not supported" >&2
  421.     exit 3
  422.     ;;
  423.   stop)
  424.     # No-op
  425.     ;;
  426.   status)
  427.     do_status
  428.     exit $?
  429.     ;;
  430.   *)
  431.     echo "Usage: checkroot.sh [start|stop]" >&2
  432.     exit 3
  433.     ;;
  434. esac
  435.  
  436. :
  437.